{include file="index/_meta" /}
    <title>余额提现申请</title>
</head>
<body>
<div id="app">
    <van-nav-bar
            title="余额提现申请"
            left-text="返回"
            left-arrow
            right-text="余额明细"
            @click-left="window.history.back();"
            @click-right="jumpCashIndex"
    >
    </van-nav-bar>

    <div>
        <van-cell-group>
            <van-cell title="当前余额" :value="info.money-info.cash_apply"></van-cell>
            <van-cell title="申请中金额" :value="info.cash_apply"></van-cell>
            <van-cell title="提现到微信">
                <template #default>
                    <van-image :src="info.user_headimgurl" width="3rem" height="3rem"></van-image>
                    {{info.user_nickname}}
                </template>
            </van-cell>
        </van-cell-group>
        <van-cell-group style="margin-top: 15px;">
            <van-cell title="提现金额">
                <template #label>
                    <span @click="allCashClick">全部提现</span>
                </template>
                <template #default>
                    <van-field v-model="value" input-align="right" clearable type="number"></van-field>
                </template>
            </van-cell>
        </van-cell-group>
    </div>

    <div style="padding: 10px 15px;">
        <van-button type="info" block @click="cash">立即提现</van-button>
    </div>


    <van-divider :style="{ color: '#1989fa', borderColor: '#1989fa', padding: '0 16px' }">提现记录</van-divider>
    <div>
        <van-cell-group>
            <van-cell v-for="(item,index) in list" :key="index" :label="item.create_time">
                <template #title>
                    <van-icon name="gold-coin" color="#1989fa"></van-icon>
                    {{item.money}}
                </template>
                <template #default>
                    <span v-if="item.state == 10">申请中</span>
                    <span v-else-if="item.state == 20">已打款</span>
                    <span v-else>拒绝打款</span>
                </template>
            </van-cell>
        </van-cell-group>
    </div>
</div>
<script>
    new Vue({
        el: "#app",
        data(){return{
            info: [],
            list: [],
            value: 0,
        }},
        created(){
            this.getDetail()
            this.getCashList()
        },
        methods:{
            getDetail() {
                let that = this
                let user = JSON.parse(localStorage.getItem('user'));
                axios.post("{:url('store/getShopDetail')}",{shop_id:"{$Think.session.wechat.shop_id}"}).then(function (res) {
                    let ret = res.data
                    that.info = ret.data
                })
            },
            allCashClick() {
                this.value = this.info.money - this.info.cash_apply
            },
            cash() {
                let that = this

                if (this.info.money - this.info.cash_apply <= 0 || (this.info.money - this.info.cash_apply) < this.value) {
                    vant.Notify({type: 'danger', message: '余额不足，无法提现', duration: 1000})
                    return false;
                }

                vant.Dialog.confirm({
                    title: '提示',
                    message: "确定要进行提现申请吗？"
                }).then(function (ret) {
                    console.log(ret)
                    axios.post("{:url('store/cash')}",{
                        shop_id: "{$Think.session.wechat.shop_id}",
                        money: that.value
                    }).then(function (ret) {
                        let res = ret.data
                        console.log(res)
                        if (res.code == 0) {
                            vant.Notify({
                                type: 'success', message: res.msg, duration: 800, onClose: function () {
                                    that.getDetail()
                                }
                            })
                        } else {
                            vant.Notify({type: 'danger', message: res.msg, duration: 1000})
                        }
                    })
                })
            },
            getCashList() {
                let that = this
                axios.post("{:url('store/cashList')}",{shop_id:"{$Think.session.wechat.shop_id}"}).then(function (res) {
                    let ret = res.data
                    that.list = ret.data
                })
            },
            jumpCashIndex(){
                window.location.href = "{:url('store/cash_index')}"
            }
        },
    })
</script>
</body>
</html>
